// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.ProfitReport;
import com.fowo.api.model.profit.report.ProfitReportImportPo;
import com.fowo.api.model.profit.report.ProfitReportItemVo;
import com.fowo.api.model.profit.report.ProfitReportSearchParamPo;
import com.fowo.api.model.profit.report.ProfitReportVo;
import com.fowo.api.service.ProfitReportService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "利润报表")
@RequestMapping("/api/profit-report")
@RestController
public class ProfitReportController {

  @Resource
  private ProfitReportService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<ProfitReportItemVo>> search(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<ProfitReportItemVo>>> searchOptions(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<ProfitReportItemVo>> getPage(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<ProfitReportVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取名称相关的编号列表")
  @GetMapping("name/{texts}/ids")
  public R<List<Long>> getIdsByName(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByName(nameList));
  }

  @Operation(summary = "获取编号相关的名称列表")
  @GetMapping("name/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getNameByIds(idList));
  }

  @PreAuthorize("@am.canAccess('profitReportCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody ProfitReportVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('profitReportEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody ProfitReportVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('profitReportEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody ProfitReportVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('profitReportDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('profitReportDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('profitReportImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<ProfitReportImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('profitReportImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<ProfitReportImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "利润报表导入模版.xlsx");
    InputStream inputStream = new ClassPathResource(
      "templates/ProfitReport.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('profitReportExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    ProfitReportSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @Operation(summary = "看板")
  @GetMapping("bulletin-board")
  public R<List<ProfitReportItemVo>> bulletinBoard(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.bulletinBoard(search));
  }

  @Operation(summary = "看板分页")
  @GetMapping("bulletin-boardPage")
  public R<Page<ProfitReportItemVo>> bulletinBoardPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.bulletinBoardPage(search));
  }

  @Operation(summary = "看板最后更新时间")
  @GetMapping("bulletin-boardLastUpdateTime")
  public R<Date> bulletinBoardLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.bulletinBoardLastUpdateTime(search));
  }

  @Operation(summary = "MSKU")
  @GetMapping("m-s-k-u")
  public R<List<ProfitReportItemVo>> mSKU(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.mSKU(search));
  }

  @Operation(summary = "MSKU分页")
  @GetMapping("m-s-k-uPage")
  public R<Page<ProfitReportItemVo>> mSKUPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.mSKUPage(search));
  }

  @Operation(summary = "MSKU最后更新时间")
  @GetMapping("m-s-k-uLastUpdateTime")
  public R<Date> mSKULastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.mSKULastUpdateTime(search));
  }

  @Operation(summary = "ASIN")
  @GetMapping("a-s-i-n")
  public R<List<ProfitReportItemVo>> aSIN(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.aSIN(search));
  }

  @Operation(summary = "ASIN分页")
  @GetMapping("a-s-i-nPage")
  public R<Page<ProfitReportItemVo>> aSINPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.aSINPage(search));
  }

  @Operation(summary = "ASIN最后更新时间")
  @GetMapping("a-s-i-nLastUpdateTime")
  public R<Date> aSINLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.aSINLastUpdateTime(search));
  }

  @Operation(summary = "父ASIN")
  @GetMapping("father-a-s-i-n")
  public R<List<ProfitReportItemVo>> fatherASIN(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.fatherASIN(search));
  }

  @Operation(summary = "父ASIN分页")
  @GetMapping("father-a-s-i-nPage")
  public R<Page<ProfitReportItemVo>> fatherASINPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.fatherASINPage(search));
  }

  @Operation(summary = "父ASIN最后更新时间")
  @GetMapping("father-a-s-i-nLastUpdateTime")
  public R<Date> fatherASINLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.fatherASINLastUpdateTime(search));
  }

  @Operation(summary = "SKU")
  @GetMapping("s-k-u")
  public R<List<ProfitReportItemVo>> sKU(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sKU(search));
  }

  @Operation(summary = "SKU分页")
  @GetMapping("s-k-uPage")
  public R<Page<ProfitReportItemVo>> sKUPage(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sKUPage(search));
  }

  @Operation(summary = "SKU最后更新时间")
  @GetMapping("s-k-uLastUpdateTime")
  public R<Date> sKULastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sKULastUpdateTime(search));
  }

  @Operation(summary = "SPU")
  @GetMapping("s-p-u")
  public R<List<ProfitReportItemVo>> sPU(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sPU(search));
  }

  @Operation(summary = "SPU分页")
  @GetMapping("s-p-uPage")
  public R<Page<ProfitReportItemVo>> sPUPage(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sPUPage(search));
  }

  @Operation(summary = "SPU最后更新时间")
  @GetMapping("s-p-uLastUpdateTime")
  public R<Date> sPULastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.sPULastUpdateTime(search));
  }

  @Operation(summary = "店铺")
  @GetMapping("store")
  public R<List<ProfitReportItemVo>> store(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.store(search));
  }

  @Operation(summary = "店铺分页")
  @GetMapping("storePage")
  public R<Page<ProfitReportItemVo>> storePage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.storePage(search));
  }

  @Operation(summary = "店铺最后更新时间")
  @GetMapping("storeLastUpdateTime")
  public R<Date> storeLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.storeLastUpdateTime(search));
  }

  @Operation(summary = "月度汇总")
  @GetMapping("monthly-summary")
  public R<List<ProfitReportItemVo>> monthlySummary(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.monthlySummary(search));
  }

  @Operation(summary = "月度汇总分页")
  @GetMapping("monthly-summaryPage")
  public R<Page<ProfitReportItemVo>> monthlySummaryPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.monthlySummaryPage(search));
  }

  @Operation(summary = "月度汇总最后更新时间")
  @GetMapping("monthly-summaryLastUpdateTime")
  public R<Date> monthlySummaryLastUpdateTime(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.monthlySummaryLastUpdateTime(search));
  }

  @Operation(summary = "订单")
  @GetMapping("order")
  public R<List<ProfitReportItemVo>> order(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.order(search));
  }

  @Operation(summary = "订单分页")
  @GetMapping("orderPage")
  public R<Page<ProfitReportItemVo>> orderPage(
    ProfitReportSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.orderPage(search));
  }

  @Operation(summary = "订单最后更新时间")
  @GetMapping("orderLastUpdateTime")
  public R<Date> orderLastUpdateTime(ProfitReportSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    // 禁用通用数据权限过滤
    search.setDisableDataPremissions(true);
    return R.data(() -> service.orderLastUpdateTime(search));
  }
}
